﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace linkouqs.Gamekit
{
    public class UIManager
    {
        private static UIManager _instance;

        public static UIManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new UIManager();
                }
                return _instance;
            }
        }

        private Transform canvasTransform;
        private Transform CanvasTransform
        {
            get
            {
                if (canvasTransform == null)
                {
                    canvasTransform = GameObject.Find("UICanvas").transform;

                }
                return canvasTransform;
            }
        }

        private Dictionary<UIPanelType, string> panelPathDict;
        private Dictionary<UIPanelType, BasePanel> panelDict;
        public Stack<BasePanel> panelStack;

        CharacterInput[] inputs;
        private UIManager()
        {
            ParseUIPanelTypeJson();

            inputs = GameObject.FindObjectsOfType<CharacterInput>();
           
        }

        

        private void ParseUIPanelTypeJson()
        {
            panelPathDict = new Dictionary<UIPanelType, string>();

            TextAsset ta = Resources.Load<TextAsset>("UIPanelType/UIPanelType");

            UIpanelTypeJson jsonObject = JsonUtility.FromJson<UIpanelTypeJson>(ta.text);

            foreach(UIPanelInfo info in jsonObject.infoList)
            {
                panelPathDict.Add(info.panelType, info.path);
            }

           
        }

        public void PushPanel(UIPanelType panelType)
        {
            if(panelStack==null)
            {
                panelStack = new Stack<BasePanel>();
            }

            if(panelStack.Count>0)
            {
                BasePanel topPanel = panelStack.Peek();
                topPanel.OnPause();
            }

            BasePanel panel = GetPanel(panelType);

            panel.OnEnter();
            panelStack.Push(panel);

            PauseGame();
        }

        public void PopPanel()
        {
            if(panelStack==null)
            {
                panelStack = new Stack<BasePanel>();

            }

            if (panelStack.Count <= 0) return;

            BasePanel topPanel = panelStack.Pop();
            topPanel.OnExit();

            if (panelStack.Count <= 0)
            {
                ResumeGame();
                return;
            }
         

            BasePanel topPanel2 = panelStack.Peek();
            topPanel2.OnResume();
           
        }

        public  BasePanel GetPanel(UIPanelType panelType)
        {
            if(panelDict==null)
            {
                panelDict = new Dictionary<UIPanelType, BasePanel>();
            }

            BasePanel panel = panelDict.TryGet(panelType);

            if(panel==null)
            {

                string path = panelPathDict.TryGet(panelType);
                GameObject temp = Resources.Load(path) as GameObject;
                GameObject instPanel = GameObject.Instantiate(temp,CanvasTransform);            
                panelDict.Add(panelType, instPanel.GetComponent<BasePanel>());
                return instPanel.GetComponent<BasePanel>();
            }
            else
            {
                return panel;
            }

           
        }

        private void PauseGame()
        {
       
            foreach(CharacterInput temp in inputs)
            {
                temp.enabled = false;
           
            }
        }

        private void ResumeGame()
        {

            foreach (CharacterInput temp in inputs)
            {
                temp.enabled = true;

            }
        }
    }
}